package co.gov.medellin.sdep.sisdep;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

//import co.gov.medellin.sdep.sisdep.admin.service.IConfiguracionService;

/**
 * Servlet implementation class TestSpringFramework
 */
public class TestSpringFramework extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public TestSpringFramework() {
		super();
	}

	private void doService(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.err.println("TestSpringFramework.DataSource ---------------------------");
		Connection connection = null;
		DataSource dataSource = null;		
		try {

			Context context = null;

			if (null == dataSource) {
				context = new InitialContext();
				dataSource = (DataSource) context.lookup("jdbc/ds_espaciopublico");
			}

			connection = dataSource.getConnection();
		} catch (SQLException sqle) {
			System.err.println("SQL Exception: " + sqle.toString());
			sqle.printStackTrace(System.err);
		} catch (NamingException ne) {
			System.err.println("NamingException : " + ne.toString());
			ne.printStackTrace(System.err);
		} catch (Exception ex) {
			System.err.println("Exception gral : " + ex.toString());
			ex.printStackTrace(System.err);
		}finally{
			try {
				connection.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		System.err.println("TestSpringFramework.DataSource=" + dataSource);
		
		System.err.println("ANTES de EJECUTAR---------------------------");
		Object object = getServletContext().getAttribute("configuracionService");
		if(object == null){
			System.err.println("-------------------------------------------------------------------");
			System.err.println("El objeto retornado esta en null");
			System.err.println("-------------------------------------------------------------------");
		}else{
			System.err.println("-------------------------------------------------------------------");
			System.err.println("El objeto retornado NO ESTA EN null >>>>>>>>>>>>>><");
			System.err.println("-------------------------------------------------------------------");
		}
		//IConfiguracionService configuracionService = (IConfiguracionService) getServletContext().getAttribute("configuracionService");

		String respuesta = "testService. FUNCIONA SPRING..... ";
		//String respuesta = "testService. FUNCIONA SPRING..... " + configuracionService.insert("COMO QUE SI ");
		System.err.println(respuesta);

		System.err.println("testService. FUNCIONA SPRING..... ");

		response.setContentType("text/html");

		ServletOutputStream out = response.getOutputStream();
		out.println("<html>");
		out.println("<head><title>Hello World</title></head>");
		out.println("<body>");
		out.println("<h1>Hello World</h1><br/>"+respuesta);
		out.println("</body></html>");
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}

}
